package com.studio.framework.controller.monitorSystem.controller;

import com.studio.framework.controller.monitorSystem.bean.ServerGroup;
import com.studio.framework.controller.monitorSystem.dto.ServerGroupDto;
import com.studio.framework.controller.monitorSystem.service.ServerGroupService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author jujh
 * @Date 2018/7/23
 * 分组
 */
@Controller
public class ServerGroupController {

    @Autowired
    private ServerGroupService serverGroupService;

    /**
     * 条件查询所有的服务
     *
     * @return
     */
    @RequestMapping(value = "/server-group-list")
    public ModelAndView pageJump() {
        ModelAndView mv = new ModelAndView("server-group-list");
        return mv;
    }

    @RequestMapping(value = "/server-group-list1")
    @ResponseBody
    public String findAll() {
        List<ServerGroup> serverGroupList = serverGroupService.findAllByCondition();
        JSONArray json = JSONArray.fromObject(serverGroupList);
        return json.toString();
    }

    /**
     * add
     *
     * @return
     */
    @RequestMapping(value = "/create-group")
    @ResponseBody
    public String create(ServerGroupDto createGroupDto) {
        serverGroupService.create(createGroupDto);
        return "ok";
    }

    /**
     * update
     *
     * @return
     */
    @RequestMapping(value = "/update-group")
    @ResponseBody
    public String update(ServerGroupDto updateGroupDto) {
        int flag = serverGroupService.update(updateGroupDto);
        if (flag == 0) {
            return "ok";
        } else {
            return "error";
        }
    }

    /**
     * delete
     *
     * @return
     */
    @RequestMapping(value = "/delete-group")
    @ResponseBody
    public String delete(String id) {
        String result = serverGroupService.delete(id);
        if ("0".equals(result)) {
            return "ok";
        } else {
            return result;
        }
    }

    /**
     * 查询详情
     *
     * @return
     */
    public String findById() {
        return null;
    }

    /**
     * update
     *
     * @return
     */
    public String update() {
        return null;
    }
}
